"""
Case Type   : spq多机并行
Case Name   : 恢复配置的spq相关参数
Create At   : 2024/04/08
Owner       : @songjing20
Description :
    1.恢复postgresql.conf
    2.恢复pg_hba.conf
Expect      :
    1.成功
    2.成功
History     :
"""

import os
import time
import unittest

from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 > Primary_SH.get_node_num(), '非1+2以上环境不执行')
class Pubsub(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_node = Node(node='PrimaryDbUser')
        self.std1_userdb_node = Node(node='Standby1DbUser')
        self.std2_userdb_node = Node(node='Standby2DbUser')
        self.commsh_std1 = CommonSH('Standby1DbUser')
        self.commsh_std2 = CommonSH('Standby2DbUser')
        self.constant = Constant()

    def test_pubsub(self):
        self.log.info('-----恢复环境-----')

    def tearDown(self):
        text1 = '-----step1:恢复postgresql.conf;expect:成功-----'
        self.log.info(text1)
        guc_res1 = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "shared_preload_libraries = 'security_plugin'")

        guc_res2 = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "pgxc_node_name = 'dn_6001_6002_6003'")

        cmd = "sed -i '/spqplugin.cluster_map/d' $PGDATA/postgresql.conf"
        res = Primary_SH.exec_gs_ssh(cmd)
        self.log.info(res)
        restart_res = Primary_SH.restart_db_cluster()
        self.log.info(restart_res)

        text2 = '-----step2:恢复pg_hba.conf;expect:成功-----'
        self.log.info(text2)
        guc_res3 = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  all  {self.pri_userdb_node.db_user}  '
            f'{self.pri_userdb_node.db_host}/32')
        self.log.info(guc_res3)

        guc_res4 = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  all  {self.std1_userdb_node.db_user}  '
            f'{self.std1_userdb_node.db_host}/32')
        self.log.info(guc_res4)

        guc_res5 = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  all  {self.std2_userdb_node.db_user}  '
            f'{self.std2_userdb_node.db_host}/32')
        self.log.info(guc_res5)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertTrue('cluster_state   : Normal' in status, f'集群状态异常')
        self.assertTrue(guc_res1 and guc_res2, f'执行失败:{text1}')
        self.assertEqual(res.count('[SUCCESS]'), 3, f'执行失败:{text1}')
        self.assertTrue(restart_res, f'重启集群失败:{text1}')
        self.assertTrue(guc_res3 and guc_res4 and guc_res5, f'执行失败:{text2}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
